package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.PotentialStudent;
import cn.wolfcode.crm.query.PotentialStudentQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPotentialStudentService;
import cn.wolfcode.crm.util.ExportExcel;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@ControllerAdvice
@RequestMapping("potentialStudent")
public class PotentialStudentController {

    @Autowired
    private IPotentialStudentService potentialStudentService;

    @RequestMapping("view")
    public String view() {
        return "potentialStudent";
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(PotentialStudent potentialStudent) {
        JSONResult result = new JSONResult();
        try {
            potentialStudentService.saveOrUpdate(potentialStudent);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult potentialStudent(PotentialStudentQueryObject qo) {
        return potentialStudentService.query(qo);
    }
    @RequestMapping("queryIntroducerStudent")
    @ResponseBody
    public List<PotentialStudent> potentialStudent(String state) {
        return potentialStudentService.queryIntroducerStudent(state);
    }

    //校检电话
    @RequestMapping("validateTel")
    @ResponseBody
    public boolean validateTel(Long id, String tel) {
        return potentialStudentService.validateTel(id,tel);
    }

    @RequestMapping("validateQq")
    @ResponseBody
    public boolean validateQq(Long id, String qq) {
        return potentialStudentService.validateQq(id,qq);
    }


    @RequestMapping("exportXls")
    //下载xls
    public void exportXls(HttpServletResponse response) throws Exception {
        //设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");

        //表格标题名
        String title = "潜在学员表";
        //表头
        String[] namesArr = {"姓名","电话","QQ","电子邮箱","建档日期","微信","住址","学历"};
        //表头对应的字段
        String[] fields = {"name","tel","qq","email","entryDate","wechat","address","eduBackground"};
        List<PotentialStudent> potentialStudents = potentialStudentService.all();

        ExportExcel<PotentialStudent> util = new ExportExcel<>();
        util.exportExcel(title, namesArr, fields, potentialStudents, response.getOutputStream());
    }

    @RequestMapping("all")
    @ResponseBody
    public List<PotentialStudent> all(){
        return potentialStudentService.all();
    }

    @RequestMapping("updateTracking")
    @ResponseBody
    public void updateTracking(Long id) {
        potentialStudentService.updateTracking(id);
    }

    //更新转正的详细信息
    @RequestMapping("updateForEnroll")
    @ResponseBody
    public Object updateForEnroll(PotentialStudent potentialStudent,String[] employmentIntention) {
        JSONResult result = new JSONResult();
        try {
            potentialStudentService.updateForEnroll(potentialStudent,employmentIntention);
        } catch (Exception e) {
            e.printStackTrace();
            result.remark(e.getMessage());
        }
        return result;
    }

    //放入资源池
    //设置状态为true
    @RequestMapping("putPoolState")
    @ResponseBody
    public Object putPoolState(Long id) {
        JSONResult result = new JSONResult();
        try {
            potentialStudentService.putPoolState(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.remark(e.getMessage());
        }
        return result;
    }

    //潜在客户池专用
    @RequestMapping("selectByPutPoolState")
    @ResponseBody
    public List<PotentialStudent> selectByPutPoolState(QueryObject qo){
        return potentialStudentService.selectByPutPoolState(qo);
    }
}
